﻿using System.Collections.Generic;
using System.Linq;
using System.Web;
using CustomerAssistant.Core;
using CustomerAssistant.Model;
using System.Configuration;
using System;
using CustomerAssistant.DAL;
using System.IO;

namespace CustomerAssistantWebServer
{
    public class OrderClientHandle : IClientHandle
    {
        static readonly string TIME_FMT = "yyMMddhh";
        public static readonly string UID = "open_id";
        public static readonly string TOKEN = "open_token";

        public void Response(HttpRequest Request, Stream OutputStream)
        {
            if (string.Compare(Request[Util.REQUEST_KEY], RequestKey, true) == 0)
            {
                var uid = Request["uid"];
                var pwd = Request["pwd"];
                var cid = Request["cid"];
                var token = Request["token"];
                if (!Auth(uid, token))
                    return;
                var newOrder = OrderDB.Instance.GetNewOrder(int.Parse(cid));
                var list = new[]
                {
                    new OrderNoticeItem(){Name="待出票",NewCount=newOrder.WaitOutTicket,OldCount=93,NewUri="Order/HomeTicketList.aspx",OldUri="Order/HomeTicketList.aspx"},
                    new OrderNoticeItem(){Name="待作废",NewCount=newOrder.NotNullify,OldCount=23,NewUri="Order/NoticeList.aspx",OldUri="Order/NoticeList.aspx"},
                    new OrderNoticeItem(){Name="待退票",NewCount=newOrder.NotAuditOrder,OldCount=9,NewUri="Order/NoticeList.aspx",OldUri="Order/NoticeList.aspx"},
                    new OrderNoticeItem(){Name="待退款",NewCount=newOrder.AuditedOrder,OldCount=45,NewUri="Order/NoticeList.aspx",OldUri="Order/NoticeList.aspx"},
                    new OrderNoticeItem(){Name="待挂解",NewCount=newOrder.WorkCount,OldCount=8,NewUri="OrderWork/OrderWorkListAccept.aspx",OldUri="OrderWork/OrderWorkListAccept.aspx"},
                }.ToList();

                using (StreamWriter sw = new StreamWriter(OutputStream))
                {
                    sw.Write(Util.Serialize<List<OrderNoticeItem>>(list));
                    sw.Flush();
                }

            }
            else
            {
                if (NextNode != null)
                    NextNode.Response(Request, OutputStream);
            }
        }

        static bool Auth(string uid, string token)
        {
            //时间戳盐,有利于抵抗重放攻击
            var salt = DateTime.Now.ToString(TIME_FMT);
            if (string.Compare(Util.CreateMd5(salt + uid), token, false) == 0)
            {
                return true;
            }
            else
            {
                salt = DateTime.Now.AddHours(1).ToString(TIME_FMT);
                if (string.Compare(Util.CreateMd5(salt + uid), token, false) == 0)
                {
                    return true;
                }
            }
            return false;
        }

        public IClientHandle NextNode
        {
            get;
            set;
        }

        public string RequestKey
        {
            get
            {
                return "orderstat";
            }
        }
    }
}